﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Antu.Core.DataAccess;

namespace WebSys.UI.SysManage
{
    public class SqlCmd : Page
    {
        #region 实现动态重用联接
        static private Dictionary<string, IDataAccess> _dicDatabase = new Dictionary<string, IDataAccess>(StringComparer.InvariantCultureIgnoreCase);
        /// <summary>
        /// 实现动态重用联接
        /// </summary>
        /// <param name="name">联接名</param>
        /// <param name="value">联接值</param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static IDataAccess GetDicDatabase(string name, string value, bool transaction)
        {
            IDataAccess dbTemp = null;
            lock (_dicDatabase)
            {
                if (_dicDatabase.ContainsKey(name))
                {
                    dbTemp = _dicDatabase[name];
                }
                else
                {
                    dbTemp = DataFactory.GetInstance(transaction, DatabaseType.Oracle, value);
                    _dicDatabase.Add(name, dbTemp);
                }
            }
            return dbTemp;
        }
        #endregion
        private Button runsql;
        private GridView SqlGridView;
        private TextBox sqltext;
        private TextBox connstr;

        protected override void OnLoad(EventArgs e)
        {
            InitControl();
            base.OnLoad(e);
            if (!IsPostBack)
            {
                Initcy();
            }
        }

        private void InitControl()
        {
            runsql = (Button)FindControl("runsql");
            runsql.Click += runsql_Click;
            SqlGridView = (GridView)FindControl("SqlGridView");
            sqltext = (TextBox)FindControl("sqltext");
            connstr = (TextBox)FindControl("connstr");
        }

        private void runsql_Click(object sender, EventArgs e)
        {
            if (connstr.Text != "" || sqltext.Text != "" || connstr.Text != "User ID=;Password=;Data Source=")
            {
                IDataAccess ida = GetDicDatabase("SqlDefaultCmdConnectionString", connstr.Text, false);
                if (sqltext.Text.Trim().ToLower().IndexOf("delete") > -1 || sqltext.Text.Trim().ToLower().IndexOf("drop") > -1)
                {
                    Response.Write("<script>alert('数据库不能进行删除数据或删除表操作');</script>");
                }
                else
                {
                    DataTable dt;
                    ida.RunSql(sqltext.Text, out dt);
                    SqlGridView.DataSource = dt;
                    SqlGridView.DataBind();
                    Antu.Core.Logger.LogManager.Manager.OutputInfoLog("操作数据：" + sqltext.Text, new object[] { connstr.Text });
                    //Antu.Core.Logger.LogManager.Manager.OutputInfoLog("操作数据：" + sqltext.Text, new object[] { connstr.Text }, "sql");
                }
            }
            else
            {
                Response.Write("<script>alert('数据库字符串或支持sql未设置');</script>");
            }
        }

        private void Initcy()
        {
        }
    }
}